package game.editer.view;

import hx.events.MouseEvent;
import hx.events.Event;
import game.editer.ui.Config;
import hx.display.Label;
import hx.layout.AnchorLayoutData;
import hx.display.HBox;
import game.editer.ui.Button;

/**
 * 一个通用的带描述的弹窗
 */
class AlertView extends BaseView {
	public static var BUTTON_CLICK = "onButtonClick";

	public static function showView(title:String, desc:String, ?buttons:Array<String>, ?cb:String->Void):AlertView {
		var view:AlertView = new AlertView(title, 400, 300);
		view.desc = desc;
		view.buttons = buttons ?? ["确定"];
		view.show();
		view.addEventListener(BUTTON_CLICK, (e:Event) -> {
			if (cb != null)
				cb(e.data);
		});
		return view;
	}

	public var buttons:Array<String> = [];

	public var desc:String;

	public function show():Void {
		var hbox = new HBox();
		this.addChild(hbox);
		hbox.layoutData = AnchorLayoutData.bottomCenter(30);
		hbox.gap = 15;

		if (desc != null) {
			var label = new Label(desc, Config.COMMON_TEXT_FORMAT);
			this.addChild(label);
			label.layoutData = new AnchorLayoutData(null, 30, null, 30, 0, 0);
			label.horizontalAlign = CENTER;
		}

		for (str in buttons) {
			var button = new Button();
			button.label.data = str;
			hbox.addChild(button);
			button.addEventListener(MouseEvent.CLICK, (e) -> {
				var event = new Event(BUTTON_CLICK);
				event.data = button.label.data;
				this.dispatchEvent(event);
				this.parent.removeChild(this);
			});
		}

		this.topView.addChild(this);
	}
}
